<template>
  <div class="form-comment-container">
    <!-- 搜索栏 -->
    <div class="search-container" style="margin-bottom: 20px;">
      <el-input
        v-model="searchUsername"
        placeholder="请输入用户名进行搜索"
        style="width: 300px; margin-right: 10px;"
      />
      <el-input
        v-model="searchKey"
        placeholder="请输入关键词进行搜索"
        style="width: 300px; margin-right: 10px;"
      />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-button type="info" @click="handleReset">重置</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userName" label="用户名" width="180" align="center" header-align="center"></el-table-column>
      <el-table-column prop="content" label="评论内容" align="center" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="createdTime" label="评论时间" width="180" align="center" header-align="center"></el-table-column>
      <el-table-column label="操作" width="240" align="center" header-align="center">
        <template #default="{ row }">
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        background
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getCommentList, deleteComment } from '@/api/zhaokao/post'

const tableData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const searchUsername = ref('')
const searchKey = ref('')

// 获取评论列表的方法，支持搜索关键字
const fetchCommentList = async (username, keyword) => {
  try {
    const res = await getCommentList({
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      username: username, // 添加用户名搜索关键字参数
      keyword: keyword   // 添加关键词搜索关键字参数
    })
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    ElMessage.error('获取评论列表失败')
  }
}

onMounted(() => {
  fetchCommentList() // 初始化时不带搜索关键字
})

const handleDelete = async (row) => {
  try {
    await deleteComment(row.id)
    ElMessage.success('删除成功')
    fetchCommentList(searchUsername.value, searchKey.value) // 删除成功后刷新列表，并保留当前搜索条件
  } catch (error) {
    ElMessage.error('删除失败')
  }
}

const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  fetchCommentList(searchUsername.value, searchKey.value) // 改变分页大小时保留当前搜索条件
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  fetchCommentList(searchUsername.value, searchKey.value) // 改变页码时保留当前搜索条件
}

const handleSearch = () => {
  fetchCommentList(searchUsername.value, searchKey.value) // 调用 fetchCommentList 并传递搜索关键字
}

const handleReset = () => {
  searchUsername.value = '' // 清空用户名搜索关键字
  searchKey.value = ''      // 清空关键词搜索关键字
  currentPage.value = 1     // 重置页码为第一页
  fetchCommentList()        // 重新获取数据，不带搜索关键字
}
</script>

<style scoped>
.search-container {
  display: flex;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>



